package cn.kailangtu.dataspot;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;


/**
 * Created by fujf on 2019/5/14
 * Description:
 */
public class PackageUtils {
    private static PackageUtils mInstance;

    public static PackageUtils getInstance() {
        if (mInstance == null) {
            mInstance = new PackageUtils();
        }
        return mInstance;
    }

    private Context getContext(){
        return AppBasicMethod.getInstance().getContext();
    }

    /**
     * 获取版本名称
     * @return 版本名称
     */
    public String getVersionName() {
        String name = "无";
        try {
            //获取包管理器
            PackageManager pm = getContext().getPackageManager();
            //获取包信息
            PackageInfo packageInfo = pm.getPackageInfo(getContext().getPackageName(), 0);
            //返回版本号
            name = packageInfo.versionName;
        } catch (Exception e) {

        }
        return name;
    }

    public int get获取APP版本号() {
        int no = -1;
        try {
            //获取包管理器
            PackageManager pm = getContext().getPackageManager();
            //获取包信息
            PackageInfo packageInfo = pm.getPackageInfo(getContext().getPackageName(), 0);
            //返回版本号
            no = packageInfo.versionCode;
        } catch (Exception e) {

        }
        return no;
    }

    public String get获取App名称() {
        String name = "无";
        try {
            //获取包管理器
            PackageManager pm = getContext().getPackageManager();
            //获取包信息
            PackageInfo packageInfo = pm.getPackageInfo(getContext().getPackageName(), 0);
            //获取应用 信息
            ApplicationInfo applicationInfo = packageInfo.applicationInfo;
            //获取albelRes
            int labelRes = applicationInfo.labelRes;
            //返回App的名称
            name = getContext().getResources().getString(labelRes);
        } catch (Exception e) {

        }
        return name;
    }

    public String get获取App包名() {
        String name = "无";
        try {
            name = getContext().getPackageName();
        } catch (Exception e) {

        }
        return name;
    }


}
